package com.qianxun.module.datasource1.admin.rest;

import com.qianxun.module.datasource1.admin.service.SysDataModifyLockService;
import com.qianxun.module.datasource1.admin.util.SysUserUtil;
import com.qianxun.module.base.constant.Result;
import com.qianxun.framework.common.util.ObjectUtil;
import com.qianxun.module.datasource1.admin.service.SysUserService;
import com.qianxun.module.datasource1.admin.entity.SysUser;
import com.qianxun.module.datasource1.admin.dto.SysUserDTO;
import com.qianxun.module.base.bo.PageInfo;
import com.qianxun.plugins.shiro.ShiroUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* com.zch.admin.base.entity.SysUser模型http接口处理
*
* @author: admin
* @date: 2019-11-13
* @version: 1.0.0
*/
@RestController
@RequestMapping("/admin/user")
public class SysUserRestController {
    @Resource
    private SysUserService baseService;
    @Resource
    private SysDataModifyLockService sysDataModifyLockService;


    @PostMapping(value = {"/add"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result add(SysUserDTO query) {
        baseService.add(query);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result deleteById(@PathVariable(value = "id") Long id) {
        sysDataModifyLockService.checkLocked(SysUserUtil.dataLockKey(id));

        baseService.deleteById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result updateById(@PathVariable(value = "id") Long id, SysUserDTO query) {
        sysDataModifyLockService.checkLocked(SysUserUtil.dataLockKey(id));

        SysUser SysUser = new SysUser();
        ObjectUtil.switchAllFieldValue(SysUser, query);
        baseService.updateById(SysUser);
        return Result.success();
    }

    @PostMapping(value = {"/set/password"})
    public Result setPassword(@RequestParam(value = "oldPassword") String oldPassword ,@RequestParam(value = "password")String password){
        long id = ShiroUtil.getUser(SysUser.class).getId();
        sysDataModifyLockService.checkLocked(SysUserUtil.dataLockKey(id));

        baseService.setPassword(oldPassword ,password);
        return Result.success();
    }

    @RequestMapping(value = {"/search/page"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchPage(@RequestParam(required = false) Integer current ,@RequestParam(required = false) Integer size){
        PageInfo pageInfo = PageInfo.build(current,size);

        IPage<SysUser> page =  baseService.page(new Page<>(pageInfo.getCurrent() ,pageInfo.getSize()));
        return Result.success(page).setCode(0);
    }

    @RequestMapping(value = {"/search/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchOneById(@PathVariable(value = "id") Long id) {
        SysUser SysUser = baseService.getById(id);
        return Result.success(SysUser);
    }
}
